<template>
  <el-container>
    <el-header class="header-wrapped">
      <div class="header-content">
        <h3>温州台风网</h3>
        <span class="welcome">欢迎您的登录</span>
      </div>
    </el-header>

    <el-main>
      <div class="login-wrapped">
        <div class="box-card">
          <el-tabs v-model="activeName" class="demo-tabs" stretch>
            <el-tab-pane label="登录" name="first">
              <el-form class="login-form">
                <el-form-item label="账号">
                  <el-input v-model="loginData.account" placeholder="请输入账号" />
                </el-form-item>
                <el-form-item label="密码">
                  <el-input v-model="loginData.password" type="password" placeholder="请输入密码" show-password />
                </el-form-item>
                <!-- 底部外壳 -->
                <div class="footer-wrapped">
                  <div class="forget-password">
                    <span class="forget-password-button">忘记密码</span>
                  </div>
                  <div class="footer-button">
                    <el-button type="primary" class="login-button" @click="Login">登录</el-button>
                  </div>
                  <div class="footer-go-register">
                    <span>没有账号?</span>
                    <span class="go-register-button" @click="activeName = 'second'">立即注册</span>
                  </div>
                </div>
              </el-form>
            </el-tab-pane>
            <el-tab-pane label="注册" name="second">
              <el-form class="login-form">
                <el-form-item label="账号">
                  <el-input v-model="registerData.account" placeholder="账号长度6-12位" />
                </el-form-item>
                <el-form-item label="密码">
                  <el-input v-model="registerData.password" placeholder="密码需长度6-12位含字母数字" />
                </el-form-item>
                <el-form-item label="确认密码">
                  <el-input v-model="registerData.rePassword" placeholder="请再次输入密码" />
                </el-form-item>
                <div class="footer-button">
                  <el-button type="primary" @click="Register">注册</el-button>
                </div>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>


    </el-main>
    <el-footer class="footer-wrapped">
      <div class="footer-content">
        <div class="title">
          <span>Copyright © 2024 cws</span>
        </div>
      </div>
    </el-footer>
  </el-container>
</template>
<script setup lang="ts">
import { login, register } from '@/api/login';
import { reactive } from 'vue';
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const activeName = ref('first')

interface FormData {
  account: string | null;
  password: string;
  rePassword?: string;//?的作用表示可选
}

//登录功能
const loginData: FormData = reactive({
  account: 's12345',
  password: 's12345'
})
const router = useRouter()
const Login = async () => {
  const res = await login(loginData)
  if (res.message == '登录成功') {
    ElMessage({
      message: res.message,
      type: 'success',
    })
    router.push('/home')
  } else {
    ElMessage.error(res.message)
  }
}



//注册 
// 注册表单数据
const registerData: FormData = reactive({
  account: null,
  password: '',
  rePassword: '',
})
const Register = async () => {
  if (registerData.password == registerData.rePassword) {
    const res = await register(registerData)
    if (res.message == '注册成功') {
      ElMessage({
        message: res.message,
        type: 'success',
      })
      activeName.value = 'first'
    } else {
      ElMessage.error(res.message)
    }
  } else {
    ElMessage.error('两次密码不一致')
  }
}
</script>
<style scoped lang='scss'>
//头部外壳
.header-wrapped {

  //头部内容
  .header-content {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;

    //欢迎语
    .welcome {
      font-size: 13px;
    }
  }
}

.el-main {
  background-image: url('@/assets/bg.png');
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  width: 100%;
  height: 600px;


  .login-wrapped {
    // width: 1200px;
    // height: 600px;
    margin: 0 auto;


    .box-card {
      width: 350px;
      height: 375px;
      margin-right: 100px;
      margin-top: 100px;
      border-radius: 10px;
      padding: 20px;
      background-color: #fff;
      float: right;
      position: relative;
      // top: 14%;

      .login-form {
        .footer-button {
          margin-top: 10px;
          display: flex;
          justify-content: center;
        }

        .footer-wrapped {
          display: flex;
          flex-direction: column;


          .forget-password {
            display: flex;
            justify-content: flex-end;
            margin-top: 10px;

            .forget-password-button {
              font-size: 12px;
              color: #409eff;
              cursor: pointer;
            }
          }


          .footer-go-register {
            display: flex;
            justify-content: center;
            margin-top: 12px;
            font-size: 12px;

            .go-register-button {
              color: #409eff;
              cursor: pointer;
            }
          }
        }

      }
    }
  }
}

.footer-wrapped {
  margin-top: 10px;

  // 底部内容
  .footer-content {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;

    span {
      color: #666;
      font-size: 12px;
    }
  }
}

.el-form {
  margin-top: 30px;
}

//tabs标签
:deep(.el-tabs__item) {
  //样式穿透
  color: #333;
  font-size: 18px;
}

//账号密码字体高度
:deep(.el-form-item__label) {
  font-size: 15px;
  line-height: 42px;
  color: #333;
}

//input高度
:deep(.el-input__inner) {
  height: 40px;
}

:deep(.el-button) {
  width: 300px;
  height: 45px;
  font-size: 16px;
}
</style>
